'use strict';

const { apiSuccess } = require('../extend/context');

const Controller = require('egg').Controller;

class ManagerController extends Controller {
  //管理员列表
  async index() {
    let page = this.ctx.request.body.page ? parseInt(this.ctx.request.body.page) : 1
    let limit = parseInt(this.ctx.request.body.limit);
    console.log(page, limit)
    let offset = (page - 1) * limit;
    const res = await this.app.model.Manager.findAll({
      offset,
      limit
    })
    let count = await this.app.model.Manager.count();
    console.log(count, "count")
    let obj = {
      data: res,
      count: count
    }
    this.ctx.apiSuccess(
      obj, "获取成功", 200
    )
  }
  //新增管理员
  async addmanager() {
    let { username, password } = this.ctx.request.body;
    this.ctx.validate({
      username: {
        type: "string",
        required: false,
        defValue: "",
        desc: "用户名称",
      },
      password: { type: "string", required: true, desc: "密码" },
      name: {
        type: "string",
        required: true,
        defValue: "",
        desc: "姓名",

      },
      notename: {
        type: "string",
        required: true,
        defValue: "",
        desc: "简历名",

      },
      note: {
        type: "string",
        required: true,
        defValue: "",
        desc: "简历",

      },
      avatar: {
        type: "string",
        required: true,
        defValue: "",
        desc: "头像",

      }
    })
    if (await this.app.model.Manager.findOne({
      where: {
        username
      }
    })) {
      this.ctx.throw("该用户已存在", "该用户已存在", 400)
    }
    let res=await this.app.model.Manager.create({
      username,password,
      note:this.ctx.request.body.note,
      notename:this.ctx.request.body.notename,
      name:this.ctx.request.body.name,
      avatar:this.ctx.request.body.avatar
    })
    if(!res){
      this.ctx.throw(res,"创建失败",500)
    }
    this.ctx.apiSuccess(res,"创建成功",200)
  }
  async editmanager(){
    let { username, password } = this.ctx.request.body;
    this.ctx.validate({
      id:{  type: "int",
        required: true,
        defValue: "",
        desc: "id",
        
      },
      username: {
        type: "string",
        required: true,
        defValue: "",
        desc: "用户名称",
      },
      password: { type: "string", required: true, desc: "密码" },
      name: {
        type: "string",
        required: true,
        defValue: "",
        desc: "姓名",

      },
      notename: {
        type: "string",
        required: true,
        defValue: "",
        desc: "简历名",

      },
      note: {
        type: "string",
        required: true,
        defValue: "",
        desc: "简历",

      },
      avatar: {
        type: "string",
        required: true,
        defValue: "",
        desc: "头像",

      }
    })
    let res=await this.app.model.Manager.findOne({
      where:{
        id:this.ctx.request.body.id
      }
    })
    if(!res){
      this.ctx.throw(res,"该用户不存在",500)
    }
    res.username=this.ctx.request.body.username;
    res.password=this.ctx.request.body.password;
    res.note=this.ctx.request.body.note;
    res.notename=this.ctx.request.body.notename;
    res.name=this.ctx.request.body.name;
    res.avatar=this.ctx.request.body.avatar;
    res.save()
this.ctx.apiSuccess(res,"修改成功",200)
  }
  async deleteUser(){
    this.ctx.validate({
      id:{  type: "int",
        required: false,
        defValue: "",
        desc: "id",
        
      },
    })
    let res=this.app.model.Manager.destroy({
      where:{
        id:this.ctx.request.body.id
      }
    })
    this.ctx.apiSuccess(res,"删除成功",200)
  }
}

module.exports = ManagerController;
